﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
using System.Threading.Tasks;

namespace 练习
{
    class Program
    {
        static void Main(string[] args)
        {
            List<person> plist = new List<person>()
            {new person(){
                 id=1,
                 Name="张三",
                  Gender="女"
            },
            new person(){
                 id=2,
                 Name= "李四",
                  Gender="男" 
            }
            };
            #region 以二进制的形式序列化对象 binary
            using (Stream fs = new FileStream("F:\\binaryserialize.txt", FileMode.Create, FileAccess.Write))
            {

                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, plist);
            }
            using (Stream rs = new FileStream("F:\\binaryserialize.txt", FileMode.Open, FileAccess.Read))
            {
                BinaryFormatter bf = new BinaryFormatter();
                List<person> pp = (List<person>)bf.Deserialize(rs);
            }
            #endregion

            #region 以soap的形式序列化对象
            using (Stream str = new FileStream("F:\\soapseralize.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                SoapFormatter soap = new SoapFormatter();
                //soap.Serialize(str, plist);//soap不能转换泛型集合
                soap.Serialize(str, plist.ToArray());
            }
            using (Stream stw = new FileStream("F:\\soapseralize.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                SoapFormatter soap = new SoapFormatter();
                person[] p = (person[])soap.Deserialize(stw);
            }
            #endregion

            #region xml序列化
            using (Stream stw = new FileStream("F:\\xmlseralize.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                XmlSerializer xml = new XmlSerializer(typeof(List<person>));
                stw.Position = 0;
                xml.Serialize(stw, plist);
            }
            using (Stream str = new FileStream("F:\\xmlseralize.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                XmlSerializer xml = new XmlSerializer(typeof(List<person>));
                str.Position = 0;
                List<person> pt = (List<person>)xml.Deserialize(str);
            }
            #endregion

            #region json序列化与反序列化
            pp c = new pp();
            Action<string> hehe = c.xixi;
            hehe.Invoke("hehe");
            hehe.BeginInvoke(null,null,"xixi");
            Console.ReadKey();
            #endregion


            #region 再次练习文件的读写
            //using (FileStream fsr = new FileStream("F:\\binaryserialize.txt", FileMode.Open, FileAccess.Read))
            //{

            //    using (FileStream fsw = new FileStream("F:\\hehe.txt", FileMode.Create, FileAccess.Write))
            //    {
            //        byte[] buffer = new byte[1024*1024*3];
            //        while (true)
            //        {
            //            int result = fsr.Read(buffer, 0, buffer.Length);
            //            if (result == 0)
            //            {
            //                break;
            //            }
            //            fsw.Write(buffer, 0, result);
            //        }
            //    }
            //} 
            #endregion

        }

    }

    public class pp
    {
        public void xixi(string ss)
        {
            Console.WriteLine("hehe ");
        }
    }

    [Serializable]
    public class person
    {
        public int id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
    }
}
